package com.aaa.dao.impl;

import com.aaa.dao.BaseDao;
import com.aaa.dao.StaffDao;
import com.aaa.entity.Staff;
import org.apache.commons.lang3.StringUtils;

import java.util.List;
import java.util.Map;

public class StaffDaoImpl implements StaffDao {

    private BaseDao baseDao = BaseDao.getInstance();

    @Override
    public Staff login(String staffId, String password) {
        String sql = "select * from staff where staffId = ? and password = ? and status = 1";
        Object[] params = {staffId, password};
        List<Staff> staffList = baseDao.executeQuery(sql, params, Staff.class);
        if (staffList != null && !staffList.isEmpty()) {
            return staffList.get(0);
        }
        return null;
    }

    @Override
    public int updateStaffByStaffId(Staff staff) {
        String sql = "update staff set staffName = ?,phone = ?,idCard = ?,address = ?," +
                "status = ?,roleId = ?,momo = ? where staffId = ?";
        Object[] params = {staff.getStaffName(), staff.getPhone(),
                staff.getIdCard(), staff.getAddress(), staff.getStatus(), staff.getRoleId(),
                staff.getMomo(), staff.getStaffId()};
        int len = baseDao.executeUpdate(sql, params);
        return len;
    }


    @Override
    public List<Staff> getAllStaffInfo(Integer pageNumber, Integer pageSize, String searchId, String searchName) {
        String sql = "select s.*,r.roleName roleName from staff s,role r where s.roleId = r.id ";
        if (StringUtils.isNotBlank(searchId)) {
            sql += " and staffId = " + searchId;
        }
        if (StringUtils.isNotBlank(searchName)) {
            searchName = "%" + searchName + "%";
            sql += " and staffName like '" + searchName + "'";
        }
        sql += " limit ?,?";
        Object[] params = {pageNumber, pageSize};
        List<Staff> staff = baseDao.executeQuery(sql, params, Staff.class);
        return staff;
    }

    @Override
    public int getAllStaffInfoCount(String searchId, String searchName) {
        String sql = "select count(1) len from staff where 1 = 1";
        if (StringUtils.isNotBlank(searchId)) {
            sql += " and staffId = " + searchId;
        }
        if (StringUtils.isNotBlank(searchName)) {
            searchName = "%" + searchName + "%";
            sql += " and staffName like '" + searchName + "'";
        }
        List<Map<String, Object>> maps = baseDao.executeQuery(sql, null);
        if (maps != null && maps.size() > 0) {
            Map<String, Object> map = maps.get(0);
            Integer res = Integer.parseInt(map.get("len") + "");
            return res;
        }
        return 0;
    }

    @Override
    public int addStaffByStaffId(Staff staff) {
        String sql1 = "insert into staff (staffName,password,phone,idCard,address,createdTime,status,roleId," +
                "momo,enable) values (?,?,?,?,?,?,?,?,?,?)";
        Object[] params = {staff.getStaffName(), staff.getPassword(), staff.getPhone(),
                staff.getIdCard(), staff.getAddress(), staff.getCreatedTime(), staff.getStatus(), staff.getRoleId(),
                staff.getMomo(), staff.getEnable()};
        int newDataId = baseDao.executeInsert(sql1, params);

        String sql2 = "select staffId from staff where id<" + newDataId;
        List<Staff> staffList = baseDao.executeQuery(sql2,null,Staff.class);
        int max = 0;
        for(Staff staff1 : staffList){
            if(staff1.getStaffId()!=null){
                if(max < staff1.getStaffId()){
                    max = staff1.getStaffId();
                }
            }
        }

        String sql3 = "update staff set staffId = ? where id =?";
        Object[] params3 ={max+1,newDataId};
        int len = baseDao.executeUpdate(sql3,params3);
        return len;
    }

    @Override
    public int deleteStaffById(int id) {
        String sql = "delete from staff where id = ?";
        Object[] params = {id};
        int len = baseDao.executeUpdate(sql, params);
        return len;
    }
}
